﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using EA.DTO;
using EA.DAO;
using EA.Common;


namespace EA.BA
{
    public class ProductBA : ObjectBA
    {
        public List<ProductTO> GetAllProduct(int page) 
        {
            List<ProductTO> list = new List<ProductTO>();
            int index1 = (page - 1) * Constant.TOTAL_RECORD_VIEW + 1;
            int index2 = index1 + Constant.TOTAL_RECORD_VIEW - 1;
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetListProduct(index1,index2);
            for (int i = 0; i < table1.Rows.Count; i++) 
            {
                ProductTO productTO = new ProductTO();
                productTO.ID =(int)table1.Rows[i]["ID"];
                productTO.Name=table1.Rows[i]["Name"].ToString();
                productTO.ProductCategoryID =Converts.getValueInteger("ProductCategoryID",i,table1);
                productTO.Detail = Converts.getValueString("Detail",i,table1);
                productTO.FloorPrice=Converts.getValueDecimal("FloorPrice",i,table1);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice",i,table1);
                productTO.TotalBidDay=Converts.getValueInteger("TotalBidDay",i,table1);
                productTO.UpdateDate=Converts.getValueDateTime("UpdateDate",i,table1);
                productTO.BeginDate=Converts.getValueDateTime("BeginDate",i,table1);
                productTO.BuyerID=Converts.getValueInteger("BuyerID",i,table1);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice",i,table1);
                productTO.SellerID=Converts.getValueInteger("SellerID",i,table1);
                productTO.SellerRating=Converts.getValueInteger("SellerRating",i,table1);
                productTO.BuyerRating=Converts.getValueInteger("BuyerRating",i,table1);
                productTO.Status=Converts.getValueInteger("Status",i,table1);
                list.Add(productTO);
            }
            return list;
        }

        public List<ProductTO> GetAllProductByKeyword(int page,String keyword)
        {
            List<ProductTO> list = new List<ProductTO>();
            int index1 = (page - 1) * Constant.TOTAL_RECORD_VIEW + 1;
            int index2 = index1 + Constant.TOTAL_RECORD_VIEW - 1;
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetListProductByKeyword(index1, index2, keyword);
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                ProductTO productTO = new ProductTO();
                productTO.ID = (int)table1.Rows[i]["ID"];
                productTO.Name = table1.Rows[i]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", i, table1);
                productTO.Detail = Converts.getValueString("Detail", i, table1);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", i, table1);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", i, table1);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", i, table1);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", i, table1);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", i, table1);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", i, table1);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", i, table1);
                productTO.SellerID = Converts.getValueInteger("SellerID", i, table1);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", i, table1);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", i, table1);
                productTO.Status = Converts.getValueInteger("Status", i, table1);
                productTO.Url = table1.Rows[i]["Url"].ToString();
                list.Add(productTO);
            }
            return list;
        }

        public List<ProductTO> GetNewestListProduct(int top)
        {
            List<ProductTO> list = new List<ProductTO>();
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetNewestListProduct(top);
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                ProductTO productTO = new ProductTO();
                productTO.ID = (int)table1.Rows[i]["ID"];
                productTO.Name = table1.Rows[i]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", i, table1);
                productTO.Detail = Converts.getValueString("Detail", i, table1);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", i, table1);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", i, table1);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", i, table1);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", i, table1);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", i, table1);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", i, table1);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", i, table1);
                productTO.SellerID = Converts.getValueInteger("SellerID", i, table1);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", i, table1);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", i, table1);
                productTO.Status = Converts.getValueInteger("Status", i, table1);
                productTO.Url = table1.Rows[i]["Url"].ToString();
                list.Add(productTO);
            }
            return list;
        }

        public int GetTotalAllProduct()
        {
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetTotalProduct();
            return Converts.getValueInteger("Total",0,table1);
        }

        public int GetTotalAllProductByKeyword(String keyword)
        {
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetTotalProductByKeword(keyword);
            return Converts.getValueInteger("Total", 0, table1);
        }

        public List<ProductTO> GetPopularListProduct(int top)
        {
            List<ProductTO> list = new List<ProductTO>();
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetListProductAreLike(top);
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                ProductTO productTO = new ProductTO();
                productTO.ID = (int)table1.Rows[i]["ID"];
                productTO.Name = table1.Rows[i]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", i, table1);
                productTO.Detail = Converts.getValueString("Detail", i, table1);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", i, table1);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", i, table1);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", i, table1);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", i, table1);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", i, table1);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", i, table1);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", i, table1);
                productTO.SellerID = Converts.getValueInteger("SellerID", i, table1);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", i, table1);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", i, table1);
                productTO.Status = Converts.getValueInteger("Status", i, table1);
                productTO.Url = table1.Rows[i]["Url"].ToString();
                list.Add(productTO);
            }
            return list;
        }

        public List<ProductTO> GetFavouriteListProduct(int top)
        {
            List<ProductTO> list = new List<ProductTO>();
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetListProductAreRating(top);
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                ProductTO productTO = new ProductTO();
                productTO.ID = (int)table1.Rows[i]["ID"];
                productTO.Name = table1.Rows[i]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", i, table1);
                productTO.Detail = Converts.getValueString("Detail", i, table1);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", i, table1);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", i, table1);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", i, table1);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", i, table1);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", i, table1);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", i, table1);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", i, table1);
                productTO.SellerID = Converts.getValueInteger("SellerID", i, table1);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", i, table1);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", i, table1);
                productTO.Status = Converts.getValueInteger("Status", i, table1);
                productTO.Url = table1.Rows[i]["Url"].ToString();
                list.Add(productTO);
            }
            return list;
        }

        public List<ProductTO> GetListProductByCategory(int top,int categoryId)
        {
            List<ProductTO> list = new List<ProductTO>();
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetListProductByCategory(top, categoryId);
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                ProductTO productTO = new ProductTO();
                productTO.ID = (int)table1.Rows[i]["ID"];
                productTO.Name = table1.Rows[i]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", i, table1);
                productTO.Detail = Converts.getValueString("Detail", i, table1);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", i, table1);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", i, table1);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", i, table1);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", i, table1);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", i, table1);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", i, table1);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", i, table1);
                productTO.SellerID = Converts.getValueInteger("SellerID", i, table1);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", i, table1);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", i, table1);
                productTO.Status = Converts.getValueInteger("Status", i, table1);
                productTO.Url = table1.Rows[i]["Url"].ToString();
                list.Add(productTO);
            }
            return list;
        }

        public List<ProductTO> GetListProductByCategoryAndRating(int top, int categoryId)
        {
            List<ProductTO> list = new List<ProductTO>();
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetListProductByCategoryAndRating(top, categoryId);
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                ProductTO productTO = new ProductTO();
                productTO.ID = (int)table1.Rows[i]["ID"];
                productTO.Name = table1.Rows[i]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", i, table1);
                productTO.Detail = Converts.getValueString("Detail", i, table1);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", i, table1);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", i, table1);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", i, table1);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", i, table1);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", i, table1);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", i, table1);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", i, table1);
                productTO.SellerID = Converts.getValueInteger("SellerID", i, table1);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", i, table1);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", i, table1);
                productTO.Status = Converts.getValueInteger("Status", i, table1);
                productTO.Url = table1.Rows[i]["Url"].ToString();
                list.Add(productTO);
            }
            return list;
        }

        public List<ProductTO> GetAllProduct()
        {
            List<ProductTO> list = new List<ProductTO>();
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            DataTable table1 = productDA.GetListProduct();
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                ProductTO productTO = new ProductTO();
                productTO.ID = (int)table1.Rows[i]["ID"];
                productTO.Name = table1.Rows[i]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", i, table1);
                productTO.Detail = Converts.getValueString("Detail", i, table1);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", i, table1);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", i, table1);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", i, table1);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", i, table1);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", i, table1);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", i, table1);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", i, table1);
                productTO.SellerID = Converts.getValueInteger("SellerID", i, table1);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", i, table1);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", i, table1);
                productTO.Status = Converts.getValueInteger("Status", i, table1);
                list.Add(productTO);
            }
            return list;
        }

        public ProductTO GetProductByID(int ID)
        {
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            ProductTO productTO = null;
            DataTable table = productDA.GetProductByID(ID);
            if (table.Rows.Count != 0)
            {
                productTO = new ProductTO();
                productTO.ID = (int)table.Rows[0]["ID"];
                productTO.Name = table.Rows[0]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", 0 , table);
                productTO.Detail = Converts.getValueString("Detail", 0 , table);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", 0 , table);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", 0 , table);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", 0 , table);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", 0 , table);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", 0 , table);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", 0 , table);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", 0 , table);
                productTO.SellerID = Converts.getValueInteger("SellerID", 0 , table);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", 0 , table);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", 0, table);
                productTO.Status = Converts.getValueInteger("Status", 0, table);
                productTO.TotalLike = Converts.getValueInteger("TotalLike", 0, table);
            }
            return productTO;
        }

        public ProductTO GetValidAuctionProductByID(int ID)
        {
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            ProductTO productTO = null;
            DataTable table = productDA.GetValidAuctionProductByID(ID);
            if (table.Rows.Count != 0)
            {
                productTO = new ProductTO();
                productTO.ID = (int)table.Rows[0]["ID"];
                productTO.Name = table.Rows[0]["Name"].ToString();
                productTO.ProductCategoryID = Converts.getValueInteger("ProductCategoryID", 0, table);
                productTO.Detail = Converts.getValueString("Detail", 0, table);
                productTO.FloorPrice = Converts.getValueDecimal("FloorPrice", 0, table);
                productTO.StepPrice = Converts.getValueDecimal("StepPrice", 0, table);
                productTO.TotalBidDay = Converts.getValueInteger("TotalBidDay", 0, table);
                productTO.UpdateDate = Converts.getValueDateTime("UpdateDate", 0, table);
                productTO.BeginDate = Converts.getValueDateTime("BeginDate", 0, table);
                productTO.BuyerID = Converts.getValueInteger("BuyerID", 0, table);
                productTO.CurrentPrice = Converts.getValueDecimal("CurrentPrice", 0, table);
                productTO.SellerID = Converts.getValueInteger("SellerID", 0, table);
                productTO.SellerRating = Converts.getValueInteger("SellerRating", 0, table);
                productTO.BuyerRating = Converts.getValueInteger("BuyerRating", 0, table);
                productTO.Status = Converts.getValueInteger("Status", 0, table);
                productTO.TotalLike = Converts.getValueInteger("TotalLike", 0, table);
            }
            return productTO;
        }

        public Boolean DeleteProduct(int ID)
        {
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            return productDA.DeleteProduct(ID);
        }

        public Boolean UpdateProductByParam(int ID, int status, DateTime beginDate, int productCategoryID)
        { 
            ProductDA productDA=new ProductDA(ObjectBA.ConnectionString);
            return productDA.UpdateProductByParam(ID, status, beginDate, productCategoryID);
        }

        public Boolean UpdateProductByPlusMinute(int ID, DateTime currentDate)
        {
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            return productDA.UpdateProductByPlusMinute(ID, currentDate);
        }

        public Boolean UpdateProductByCurrentPrice(int ID,Decimal currentPrice)
        {
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);
            return productDA.UpdateProductByCurrentPrice(ID, currentPrice);
        }

        public Boolean UpdateRatingByID(int ID, int rating)
        {
            return new ProductDA(ObjectBA.ConnectionString).UpdateRatingByID(ID, rating);
        }

        public Boolean InsertProduct(ProductTO productTO)
        {
            return new ProductDA(ObjectBA.ConnectionString).InsertProduct(productTO);
        }
        public Boolean InsertProductImage(ProductImageTO productTO)
        {
            return new ProductDA(ObjectBA.ConnectionString).InsertProductImage(productTO);
        }

        public int GetLastestProduct()
        {
            ProductDA productDA = new ProductDA(ObjectBA.ConnectionString);

            DataTable table = productDA.GetLastestProduct();
            if (table.Rows.Count != 0)
            {
                return Converts.getValueInteger("ID",0, table);
            }
            return 0;
        }
    }
}
